export default async function dialogToPrint(dialogRef, hiddens = [], A4width) {
    try {
        dialogRef = dialogRef || this.$refs.dialog;

        const dialogEl = dialogRef.$el;
        dialogRef.setFullscreen(true);

        const width = dialogEl.style.width;

        // A4 纸的宽度
        dialogEl.style.width = A4width || '210mm';

        // 隐藏footer
        hiddens = hiddens.length === 0 ? Array.from(document.querySelectorAll('.dialog-footer')) : hiddens;

        // 缓存style.display
        const hiddenNodeDisplays = hiddens.map(item => item.style.display);

        // 隐藏dom
        hiddens.forEach(item => (item.style.display = 'none'));

        setTimeout(() => {
            // 同步任务
            window.print();
        }, 500);
        await new Promise(resolve =>
            setTimeout(() => {
                // 恢复原来的宽度
                dialogEl.style.width = width;

                // 取消全屏
                dialogRef.setFullscreen(false);

                // 恢复footer
                hiddens.forEach((item, index) => (item.style.display = hiddenNodeDisplays[index]));
                resolve();
            }, 1000)
        );

        dialogRef = null;
        hiddenNodeDisplays.length = 0;
        hiddens.length = 0;
    } catch (error) {
        console.log('toprint error :>> ', error);
        throw new Error(error);
    }
}
